package arithmetic;

/**
 * @author Ajie
 * @date 2021/9/9
 * @function 王刚美团小球，red 3 ，blue 3, yellow 3. 9宫格。 同色不能相邻。
 */
public class A_RGB_3 {
    public static void main(String[] args) {

        int[][][] res = new int[100][3][3];//结果缓存
        int[][] dp = new int[3][3];

        getResultNum(res, dp, 0, 0, 0, 0);

        System.out.println(" resultNum --->>>>>>>" + resultNum);
//        //打印结果
//        for (int i = 0; i < resultNum; i++) {
//            int[][] dpr = res[i];
//            extracted(dpr);
//            System.out.println("--------------");
//        }
    }

    static int resultNum;

    //row 2->0 ,col 0->2.
    private static void getResultNum(int[][][] res, int[][] dp,int index, int red, int blue, int yellow) {

        //退出结果。
        if (index == 9) {
            //结果
            dp.clone();//保存结果。
            resultNum++;//记录数量。
            extracted(dp);
        }

        int col = index % 3;
        int row = 2 - index / 3;

        //这个位置可以的选项。
        int[] colors = getColorsAble(dp, row, col, red, blue, yellow);
        for (int i = 0; i < colors.length; i++) {
            int tempRed = red;
            int tempBlue = blue;
            int tempYellow = yellow;
            int cn = colors[i];
            if (cn != 0) {

                //当前位置可能的选项
                dp[row][col] = cn;
                if (cn == 1) tempRed++;
                if (cn == 2) tempBlue++;
                if (cn == 3) tempYellow++;

                getResultNum(res, dp, index + 1, tempRed, tempBlue, tempYellow);

                //回退。因为 下一次循环重新设置了。
                dp[row][col] = 0;

            }
        }

    }

    private static int[] getColorsAble(int[][] dp, int row, int col, int red, int blue, int yellow) {
        int[] colors = {0, 1, 2, 3};//异常，r b y
        int up = 0;
        if (row + 1 > 2) {//上边界
        } else {
            up = dp[row + 1][col];
        }

        int left = 0;
        if (col - 1 < 0) {//左边界
        } else {
            left = dp[row][col - 1];
        }
        colors[up] = 0;
        colors[left] = 0;
        for (int i = 0; i < colors.length; i++) {
            int cn = colors[i];
            if (cn != 0) {
                if (cn == 1) red++;
                if (cn == 2) blue++;
                if (cn == 3) yellow++;
                if (red > 3 || blue > 3 || yellow > 3) {
                    if (cn == 1) red--;
                    if (cn == 2) blue--;
                    if (cn == 3) yellow--;
                    colors[cn] = 0;
                    continue;
                }
            }

        }
        return colors;
    }

    //打印
    private static void extracted ( int[][] dp){
            System.out.println("extracted----"+resultNum);
            for (int j = 2; j >=0; j--) {
                for (int k = 0; k < 3; k++) {
                    int num = dp[j][k];
                    char c = 'r';
                    if (num == 0) c = ' ';// 错误 初始化
                    if (num == 1) c = 'r';
                    if (num == 2) c = 'b';
                    if (num == 3) c = 'y';
                    System.out.print(c);
                    System.out.print(",");
                }
                System.out.println();
            }
        }
    }
